<?php // $Id: block_library_reserve_forms.php,v 1.0.0.0 2010-07-14 16:32 pthayer Exp $
require_once('../../config.php');
global $CFG, $USER, $COURSE;
$get_reserve = stripslashes($_GET['get_reserve']);
$level = stripslashes($_GET['level']);
$data = stripslashes($_GET['data']);
$selection = stripslashes($_GET['selection']);
$sel_id = stripslashes($_GET['sel_id']);

$back = $level-1;
$next = $level+1;
$json_array = json_decode($data);
$form = '<form name="myform">';

//if($data){
//		$form .= '<input type="hidden" name="data" value="'.$data.'" />';
//}

switch($level) {

	case 0:
		$sql = "SELECT * FROM {$CFG->prefix}libres WHERE course={$COURSE->id}";
		$rs = get_records_sql($sql);
		$form .= '<div style="font-weight:bold;">Your pending requests:</div>';
		foreach($rs as $op){
			$form .= '<div style="background-color:#ddd;padding:2px;margin-bottom:3px;"><a href="#" onclick="get_reserve('.$op->id.', \''.$CFG->wwwroot.'\'); return false;">'.$op->title.'</a></div>';
		}
		$form .= '<div><a href="#" onclick="init_libres_form(1, \''.$CFG->wwwroot.'\'); return false;">Make a new request</a></div>';
		break;

	case 1:
		$sql = "SELECT * FROM {$CFG->prefix}request_types ORDER BY id asc";
		$rs = get_records_sql($sql);
		foreach($rs as $op){
			$json_array->req_type == $op->id?$checked = 'checked="yes"':$checked = '';
			$form .= '<input type="radio" name="req_type" value="'.$op->id.'" '.$checked.' onmouseup="fdata.user='.$USER->id.';fdata.course='.$COURSE->id.';fdata.req_type=this.value;init_libres_form(2, \''.$CFG->wwwroot.'\'); return false;" />'.$op->description.'<br />';
		}
		break;

	case 2:
		$form .= '<p>Book title:<br />';
		$form .= '<input type="text" name="title" value="'.$json_array->title.'" /></p>';
        $form .= '<p>Book author:<br />';
		$form .= '<input type="text" name="author" value="'.$json_array->author.'" /></p>';
		$form .= '<p>Publisher:<br />';
		$form .= '<input type="text" name="publisher" value="'.$json_array->publisher.'" /></p>';
		$form .= '<div style="float:left;margin-right:5px;"><a href="#" onclick="init_libres_form('.$back.', \''.$CFG->wwwroot.'\'); return false;">Back</a></div>';
		$form .= '<div style="float:left;"><a href="#" onclick="fdata.title=document.myform.title.value; fdata.author=document.myform.author.value; fdata.publisher=document.myform.publisher.value; init_libres_form('.$next.', \''.$CFG->wwwroot.'\'); return false;">Next</a></div><br style="height:0px;clear:both;" />';
		break;

    case 3:
        if($json_array->req_type == 3){
			$level = 4;
		}else{
			if($json_array->req_type == 1){$style1 = 'display:none;';}elseif($json_array->req_type == 2){$style2 = 'display:none;';}
			$form .= '<div style="'.$style2.'">Library call number:<br />';
			$form .= '<input type="text" name="call_num" value="'.$json_array->call_num.'" /></div>';
			$form .= '<div style="'.$style1.'">ISBN number:<br />';
			$form .= '<input type="text" name="isbn_num" value="'.$json_array->isbn_num.'" /></div>';
		    $form .= '<div style="float:left;margin-right:5px;"><a href="#" onclick="init_libres_form('.$back.', \''.$CFG->wwwroot.'\'); return false;">Back</a></div>';
		    $form .= '<div style="float:left;"><a href="#" onclick="fdata.call_num=document.myform.call_num.value; fdata.isbn_num=document.myform.isbn_num.value; init_libres_form('.$next.', \''.$CFG->wwwroot.'\'); return false;">Next</a></div><br style="height:0px;clear:both;" />';
		    break;
        }

    case 4:
        $form .= '<div><a href="#" onclick="init_libres_form('.$next.', \''.$CFG->wwwroot.'\'); return false;">Finish</a></div>';
        $form .= '<div style="float:left;margin-right:5px;"><a href="#" onclick="init_libres_form('.$back.', \''.$CFG->wwwroot.'\'); return false;">Back</a></div>';
        break;

	case 5:
		foreach($json_array as $key => $value){
			$c[] = $key;
			$v[] = $value;
		}
		$cols = implode(', ', $c);
		$vals = implode(', ', $v);
		if($json_array->id){
			$insert = update_record('libres', $json_array);
		}else{
			$insert = insert_record('libres', $json_array);
		}
		if(!$insert){
			$form .= "<div style=\"color:red;font-weight:bold;\">There was an error finalizing the request. Please review the provided information.</div>";
			$form .= '<div><a href="#" onclick="init_libres_form('.$back.', \''.$CFG->wwwroot.'\'); return false;">Back</a></div>';

		}else{
			$form .= "Your request has been submitted.";
			$form .= '<div><a href="#" onclick="init_libres_form(0, \''.$CFG->wwwroot.'\'); return false;">OK</a></div>';

		}
		break;

	case 6:
		$sql = "SELECT * FROM {$CFG->prefix}libres WHERE id=$get_reserve limit 1";
		$rs = get_recordset_sql($sql);
		echo json_encode($rs->fields);
		break;

	case 7:
		$sql = "UPDATE {$CFG->prefix}libres SET processing=$selection WHERE id=$sel_id";
		$rs = execute_sql($sql, false);
		$rs = get_recordset('libres', 'id', $sel_id);
		$rs = rs_fetch_next_record($rs);
		if($rs){
			echo '<p>The changes have been saved.</p>';
			echo '<input type="radio" name="req_status" id="req_status" value="0" ';
			if($rs->processing == 0){echo 'checked="true"';}
			echo " />is not being processed.<br />";
			echo '<input type="radio" name="req_status" id="req_status" value="1" ';	
			if($rs->processing == 1){echo 'checked="true"';}
			echo '/>is being processed.<br />';
			echo '<input type="radio" name="req_status" id="req_status" value="2" ';
			if($rs->processing == 2){echo 'checked="true"';}
			echo '/>has been processed.<br />';
		}else{
			echo "Sorry, an error occurred. Please refresh the page and try again.";
		}
		exit();
		break;

	default:
		$form .= "Some quirky error has occurred. How on Earth did you do that?";
		$form .= '<div style="margin-right:5px;"><a href="#" onclick="init_libres_form('.$back.', \''.$CFG->wwwroot.'\'); return false;">Back</a></div>';
}
if($level != 6){
	$form .= '</form>';
	echo $form;
}
?>
